package com.xu.sso.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xu.sso.core.enums.ErrorMessage;
import com.xu.sso.core.exception.SSOException;
import com.xu.sso.core.utils.ExceptionUtil;
import com.xu.sso.db.model.base.ApiAuth;
import com.xu.sso.db.model.base.AppRole;
import com.xu.sso.db.model.base.User;
import com.xu.sso.db.service.ApiAuthDBService;
import com.xu.sso.db.service.RoleDBService;
import com.xu.sso.db.service.UserDBService;
import com.xu.sso.server.model.request.OpenApiRequest;
import com.xu.sso.server.model.response.OpenApiResponse;
import com.xu.sso.server.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ApiServiceImpl implements ApiService {

    @Autowired
    private UserDBService userDBService;
    @Autowired
    private RoleDBService roleDBService;
    @Autowired
    private ApiAuthDBService apiAuthDBService;

    @Override
    public OpenApiResponse allUsers(OpenApiRequest request) throws SSOException {
        List<User> users = userDBService.findAllUsers();
        OpenApiResponse response = new OpenApiResponse();
        if (null != users) {
            response.setContent(JSONObject.toJSONString(users));
        }
        return response;
    }

    @Override
    public OpenApiResponse usersIn(OpenApiRequest request) throws SSOException {
        List<String> usernames = null;
        try {
            usernames = JSONObject.parseArray(request.getContent(), String.class);
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>ApiServiceImpl.usersIn content convert error... ");
            throw ExceptionUtil.createException(ErrorMessage.IllegalArgumentException);
        }
        if (null == usernames || usernames.size() == 0) {
            return null;
        }
        List<User> users = userDBService.findUsersByListIn(usernames);
        OpenApiResponse response = new OpenApiResponse();
        if (null != users) {
            response.setContent(JSONObject.toJSONString(users));
        }
        return response;
    }

    @Override
    public OpenApiResponse userByName(OpenApiRequest request) throws SSOException {
        User user = userDBService.findUserByUsername(request.getContent());
        OpenApiResponse response = new OpenApiResponse();
        if (null != user) {
            response.setContent(JSONObject.toJSONString(user));
        }
        return response;
    }

    @Override
    public OpenApiResponse userByLikeName(OpenApiRequest request) throws SSOException {
        List<User> users = userDBService.findUsersByLikeUsername(request.getContent());
        OpenApiResponse response = new OpenApiResponse();
        if (null != users) {
            response.setContent(JSONObject.toJSONString(users));
        }
        return response;
    }

    @Override
    public OpenApiResponse userRoles(OpenApiRequest request) throws SSOException {
        List<AppRole> appRoles = roleDBService.findInAppRoles(request.getAppId(), request.getContent());
        OpenApiResponse response = new OpenApiResponse();
        if (null != appRoles) {
            List<String> roles = appRoles.stream().map(AppRole::getRoleName).collect(Collectors.toList());
            response.setContent(JSONObject.toJSONString(roles));
        }
        return response;
    }

    @Override
    public String findAppKey(String appId) throws SSOException {
        ApiAuth apiAuth = apiAuthDBService.findApiAuthByAppId(appId);
        if (null == apiAuth) {
            throw ExceptionUtil.createException(ErrorMessage.AppNotAuthorization);
        }
        if (StringUtils.isBlank(apiAuth.getAppKey())) {
            throw ExceptionUtil.createException(ErrorMessage.KeyNotFound);
        }
        return apiAuth.getAppKey();
    }

}
